package com.heze.xunhuan.controller;

import com.heze.xunhuan.service.InteractionService;
import com.heze.xunhuan.utils.SecurityContextUtil;
import com.heze.xunhuan.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 互动控制器
 *
 * @author heze-xunhuan
 */
@Tag(name = "互动模块", description = "点赞和收藏相关接口")
@RestController
@RequestMapping("/api/v1/posts")
@RequiredArgsConstructor
public class InteractionController {

    private final InteractionService interactionService;

    @Operation(summary = "点赞", description = "对信息进行点赞")
    @PostMapping("/{id}/like")
    public Result<Void> like(@PathVariable Long id) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        interactionService.like(userId, id);
        return Result.success();
    }

    @Operation(summary = "取消点赞", description = "取消对信息的点赞")
    @DeleteMapping("/{id}/like")
    public Result<Void> unlike(@PathVariable Long id) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        interactionService.unlike(userId, id);
        return Result.success();
    }

    @Operation(summary = "收藏", description = "收藏信息")
    @PostMapping("/{id}/favorite")
    public Result<Void> favorite(@PathVariable Long id) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        interactionService.favorite(userId, id);
        return Result.success();
    }

    @Operation(summary = "取消收藏", description = "取消收藏信息")
    @DeleteMapping("/{id}/favorite")
    public Result<Void> unfavorite(@PathVariable Long id) {
        Long userId = SecurityContextUtil.getCurrentUserId();
        if (userId == null) {
            return Result.unauthorized("未登录");
        }
        interactionService.unfavorite(userId, id);
        return Result.success();
    }
}

